<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="任务序号" prop="taskid">
        <el-input
          v-model="queryParams.taskid"
          placeholder="请输入任务序号"
          class="home_input"
          clearable
        @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仪器编码" prop="devicenum">
        <el-input
          v-model="queryParams.devicenum"
          placeholder="请输入仪器编码"
          class="home_input"
          clearable
        @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="送检人电话" prop="telphone">
        <el-input
          v-model="queryParams.telphone"
          placeholder="请输入送检人电话"
          class="home_input"
          clearable
        @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="送检单位" prop="companyname">
        <el-input
          v-model="queryParams.companyname"
          placeholder="请输入送检单位"
          class="home_input"
          clearable
          @keyup.enter.native="handleQuery"
        />

    </el-form-item>
    <el-form-item label="送检人" prop="contactperson">
      <el-input
        v-model="queryParams.contactperson"
        placeholder="请输入送检人"
        class="home_input"
        clearable
      @keyup.enter.native="handleQuery"
      />
    </el-form-item>
    <el-form-item label="送检时间" prop="sddate">
      <el-input
        v-model="queryParams.sddate"
        placeholder="请输入送检时间"
        class="home_input"
        clearable
      @keyup.enter.native="handleQuery"
      />
    </el-form-item>
    <el-form-item label="校准编号" prop="calibration">
      <el-input
        v-model="queryParams.calibration"
        placeholder="请输入校准编号"
        class="home_input"
        clearable
      @keyup.enter.native="handleQuery"
      />
    </el-form-item>
    <el-form-item label="流转状态" prop="state">
      <el-select v-model="queryParams.state" placeholder="请选择流转状态" class="home_input">
        <el-option
          v-for="item in circulationList"
          :key="item.label"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="仪器类别" prop="devicemodel">
      <el-select v-model="queryParams.devicemodel" placeholder="请选择仪器类别" class="home_input">
        <el-option
          v-for="item in instrumentList"
          :key="item.label"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <!--    <el-form-item label="检定时间" prop="calibedTime">-->
    <!--      <el-input-->
    <!--        v-model="queryParams.calibedTime"-->
    <!--        placeholder="请输入检定时间"-->
    <!--        class="home_input"-->
    <!--        clearable-->
    <!--      @keyup.enter.native="handleQuery"-->
    <!--      />-->
    <!--    </el-form-item>-->
    <el-form-item>
      <el-button size="mini"
      @click="handleQuery">查询
    </el-button>
    <el-button size="mini"
    @click="resetQuery">重置
  </el-button>
</el-form-item>
  </el-form>

<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
  <el-button
    type="primary"
    plain
    icon="el-icon-plus"
    size="mini"
  @click="handleAdd"

  >新增
</el-button>
</el-col>
<el-col :span="1.5">
<el-button
  type="success"
  plain
  icon="el-icon-edit"
  size="mini"
  :disabled="single"
@click="handleUpdate"

>修改
</el-button>
  </el-col>
  <!--      <el-col :span="1.5">-->
  <!--        <el-button-->
  <!--          type="danger"-->
  <!--          plain-->
  <!--          icon="el-icon-delete"-->
  <!--          size="mini"-->
  <!--          :disabled="multiple"-->
  <!--          @click="handleDelete"-->
  <!--          -->
  <!--        >删除</el-button>-->
  <!--      </el-col>-->
<el-col :span="1.5">
<el-button
  type="warning"
  plain
  icon="el-icon-download"
  size="mini"
@click="handleExport"

>导出
</el-button>
  </el-col>
<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  </el-row>

<el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" align="center"/>
  <!--<el-table-column label="" align="center" prop="id"/>-->
<el-table-column label="任务序号" align="center" prop="taskid"/>
  <el-table-column label="校准编号" align="center" prop="calibration"/>
<el-table-column label="仪器型号" align="center" prop="devicetype"/>

<el-table-column label="仪器编码" align="center" prop="devicenum"/>
<el-table-column label="送检人电话" align="center" prop="telphone"/>
<el-table-column label="送检单位" align="center" prop="companyname"/>
<el-table-column label="送检人" align="center" prop="contactperson"/>
<el-table-column label="送检时间" align="center" prop="sddate"/>
<el-table-column label="单位地址" align="center" prop="address"/>
<el-table-column label="流转状态" align="center" prop="state" :formatter="circulationStatusFilter"/>
<el-table-column label="仪器类别" align="center" prop="devicemodel"/>
<el-table-column label="检定时间" align="center" prop="calibedTime"/>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template slot-scope="scope">
  <el-button
    size="mini"
    type="text"
    icon="el-icon-edit"
  @click="handleUpdate(scope.row)"

  >修改
</el-button>
<el-button
  v-if="scope.row.state == 3"
  size="mini"
  type="text"
  icon="el-icon-download"
@click="downloadList(scope.row)"

>下载
</el-button>
  <!--          <el-button-->
  <!--            size="mini"-->
  <!--            type="text"-->
  <!--            icon="el-icon-delete"-->
  <!--            @click="handleDelete(scope.row)"-->
  <!--            -->
  <!--          >删除</el-button>-->
  </template>
  </el-table-column>
  </el-table>

<pagination
v-show="total>0"
:total="total"
:page.sync="queryParams.pageNum"
:limit.sync="queryParams.pageSize"
  @pagination="getList"
  />

  <!-- 添加或修改任务报送对话框 -->
<el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>

<el-form ref="form" :model="form" :rules="rules" label-width="100px">
  <el-row>
    <el-col :span="12">
      <el-form-item label="仪器编码" prop="devicenum">
        <el-input v-model="form.devicenum" placeholder="请输入仪器编码"/>
      </el-form-item>
      <el-form-item label="送检人电话" prop="telphone">
        <el-input v-model="form.telphone" placeholder="请输入送检人电话"/>
      </el-form-item>
      <el-form-item label="送检单位" prop="companyname">
        <el-input v-model="form.companyname" placeholder="请输入送检人电话"/>
      </el-form-item>
    <el-form-item label="单位地址" prop="address">
      <el-input  v-model="form.address" placeholder="请输入单位地址"/>
    </el-form-item>
      <el-form-item label="生产单位" prop="producer">
        <el-input  v-model="form.producer" placeholder="请输入单位地址"/>
      </el-form-item>
  </el-col>
  <el-col :span="12">
    <el-form-item label="校准编号" prop="calibration">
      <el-input v-model="form.calibration" placeholder="请输入校准编号"/>
    </el-form-item>
    <el-form-item label="送检人" prop="contactperson">
      <el-input v-model="form.contactperson" placeholder="请输入送检人"/>
    </el-form-item>
    <el-form-item label="仪器型号" prop="devicetype">
      <el-input v-model="form.devicetype" placeholder="请输入仪器型号"/>
    </el-form-item>
    <el-form-item label="送检时间" prop="sddate">
      <el-date-picker
        v-model="form.sddate"
        type="date"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        placeholder="送检时间">
      </el-date-picker>
      <!--      <el-input v-model="form.sddate" placeholder="请输入"/>-->
    </el-form-item>

    <el-form-item label="仪器类别" prop="devicemodel">
      <el-select v-model="form.devicemodel" placeholder="请选择仪器类别">
        <el-option
          v-for="item in instrumentList"
          :key="item.label"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
  </el-col>
</el-row>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary"
@click="submitForm">确 定
</el-button>
<el-button
  @click="cancel">取 消
  </el-button>
  </div>
  </el-dialog>
  </div>
  </template>

<script>
import {listTask, getTask, getDown, delTask, addTask, updateTask} from "@/api/system/task";
import {listSendcheckcompanydictionary} from "../../../api/system/sendcheckcompanydictionary";

export default {
  name: "Task",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务报送表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskid: null,
        devicetype: null,
        devicenum: null,
        telphone: null,
        companyname: null,
        contactperson: null,
        sddate: null,
        address: null,
        state: null,
        devicemodel: null,
        calibedTime: null,
        calibration: null,
      },
      unitList: [],
      instrumentList: [
        {
          label: '转子式流速仪',
          value: "转子式流速仪",
        },
        {
          label: '电波流速仪',
          value: "电波流速仪",
        },
        {
          label: '超声波流速仪',
          value: "超声波流速仪",
        },
        {
          label: '直读式转子流速仪',
          value: "直读式转子流速仪",
        },
        {
          label: '流速流向仪',
          value: "流速流向仪",
        },
        {
          label: '海流计',
          value: "海流计",
        },
        {
          label: '测深仪',
          value: "测深仪",
        },
      ],
      circulationList: [
        {
          label: '未检',
          value: "1",
        },
        {
          label: '已检',
          value: "2",
        },
        {
          label: '已上传',
          value: "3",
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // taskid: [
        //   {required: true, message: "任务序号不能为空", trigger: "blur"}
        // ],
        devicetype: [
          {required: true, message: "仪器型号不能为空", trigger: "change"}
        ],
        contactperson: [
          {required: true, message: "送检人不能为空", trigger: "blur"}
        ],
        sddate: [
          {required: true, message: "送检时间不能为空", trigger: "blur"}
        ],
        companyname: [
          {required: true, message: "送检单位不能为空", trigger: "blur"}
        ],
        devicemodel: [
          {required: true, message: "仪器类别不能为空", trigger: "change"}
        ],
        devicenum: [
          {required: true, message: "仪器编码不能为空", trigger: "blur"}
        ],
        telphone: [
          {required: true, message: "送检人电话不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getUnit()
  },
  methods: {
    circulationStatusFilter(row) {
      switch (row.state) {
        case 1:
          return '未检';
          break;
        case 2:
          return '已检';
          break;
        case 3:
          return '已上传';
          break;
      }
    },
    unitChange(row) {
      if (row) {
        this.unitList.forEach(item => {
          if (item.companyName == row) {
            this.form.address = item.companySite
          }

        })
      }
    },

    //  获取 送检单位
    getUnit() {
      listSendcheckcompanydictionary({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        if (res.code === 200) {
          this.unitList = res.rows
        }
        console.log(res, 'res')
      })
    },

    /** 查询任务报送列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskid: null,
        devicetype: null,
        devicenum: null,
        telphone: null,
        companyname: null,
        contactperson: null,
        sddate: null,
        address: null,
        state: null,
        devicemodel: null,
        calibedTime: null,
        ReceiveTime: null,
        Urgent: null,
        Remark: null,
        producer: null,
        SpecificClass: null,
        SpareParts: null,
        MaintainTime: null,
        MaintainPerson: null,
        TaskIssuePerson: null,
        StartWorkData: null,
        MaintainRemark: null,
        MaintainUserParts: null,
        MaintainCount: null,
        ReportGenerated: null,
        RegisterPerson: null,
        CreateTime: null,
        DStype: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getUnit()
      this.open = true;
      this.title = "添加任务报送";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getUnit()
      const id = row.id || this.ids
      getTask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改任务报送";
      });
    },
    downloadList(row) {
      console.log(row, 'row')
      const id = row.id || this.ids
      this.download(`system/task/down/${id}`, '', `${row.devicemodel}_检测报告_${new Date().getTime()}.docx`)
      // getDown(id).then(res=>{
      //   if(res.code === 200){
      //     this.open = true
      //   }
      // })
    }
    ,

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
    ,
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除任务报送编号为"' + ids + '"的数据项？').then(function () {
        return delTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
    ,
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    }
  }
}
;
</script>
<style lang="scss">
.home_input .el-input__inner {
  background-color: #F0F0F0;
}

.el-button--default {
  background-color: #F0F0F0;

&
:hover,

&
:active,

&
:focus {
  background-color: #F0F0F0;
  color: #606266;
  border: 1px solid #f0f0f0;
}

}

</style>
